﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using Vitruvian.Serialization;
using Vitruvian.Serialization.Formatters;
using Vitruvian.Windows.Serialization.Formatters;
using Vitruvian.Logging;
using Vitruvian.Testing;

namespace SpotItComponents
{
    [ContainsTests("Components.DeckStuff")]
    [OptimisticSerialization]
    public class Card : Component
    {
        #region Private Data Members
        private static Logger logger = Logger.GetLogger(typeof(Card));
        private static Random randomizer = new Random(DateTime.Now.Millisecond);
        private static int cardSize = 400;

        private Graphics graphics = null;
        #endregion

        #region Public Properties
        [ImageFormatter]
        public Image CardImage { get; set; }
        public List<SymbolPositioning> Positionings { get; set; }
        public int NumberOfSymbols { get; set; }
        #endregion

        #region Constructor(s)
        public Card()
        {
            Positionings = new List<SymbolPositioning>();
        }

        #endregion

        #region Public Properties and Methods

        public void CreateBlankCard(int symbolCount)
        {
            CardImage = new Bitmap(cardSize, cardSize);
            graphics = Graphics.FromImage(CardImage);
            graphics.Clear(Color.Transparent);
            graphics.FillEllipse(Brushes.Black, 0, 0, cardSize, cardSize);
            graphics.FillEllipse(Brushes.White, 2, 2, cardSize-4, cardSize-4);

            Positionings = SymbolPositioning.Template(symbolCount);
            NumberOfSymbols = 0;
        }

        public void AddSymbol(Symbol symbol)
        {
            if (symbol != null && NumberOfSymbols < Positionings.Count)
            {
                SymbolPositioning p = Positionings[NumberOfSymbols];
                p.Symbol = symbol;
                p.Rotation = Convert.ToSingle(randomizer.Next(0,360));
                int symbolWidth = Convert.ToInt32(Convert.ToSingle(p.Radius) * 1.4142);
                Bitmap symbolImage = symbol.RotateImage(p.Rotation, symbolWidth );

                float x = Convert.ToSingle(p.CenterLocation.X - p.Radius);
                float y = Convert.ToSingle(p.CenterLocation.Y - p.Radius);
                graphics.DrawImage(symbolImage, x, y);

                NumberOfSymbols++;
            }
        }

        #endregion
    }
}
